package com.hxd.otc.module.my.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.TradeType;
import com.hxd.otc.dialog.ForbiddenBankCardDialog;
import com.hxd.otc.dialog.MultipleBlocksDialog;
import com.hxd.otc.module.my.bean.AdvertCoinTypeBean;
import com.hxd.otc.module.my.bean.BankCardBean;
import com.hxd.otc.module.my.bean.MyBondBean;
import com.hxd.otc.module.my.contract.AdManagementContract;
import com.hxd.otc.module.my.fragment.AdFragment;
import com.hxd.otc.module.my.presenter.AdManagementPresenter;
import com.hxd.otc.utils.StringUtil;
import com.hxd.otc.utils.UIUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lzy on 2018/6/11 0011.
 * description ${广告管理}
 */
public class AdManagementActivity extends BaseActivity<AdManagementPresenter> implements AdManagementContract.View, TabHost.OnTabChangeListener {

    @BindView(R.id.tv_publish)
    TextView mTvPublish;

    @BindView(R.id.tv_bond)
    TextView tvBond;

    @BindView(android.R.id.tabhost)
    FragmentTabHost mTabHost;

    private MultipleBlocksDialog mMultipleDialog;

    private static final int REQUEST_CODE_SET_COLLECTION_MODE = 2002;
    private ForbiddenBankCardDialog<String> mSetNickNameDialog;

    @Override
    protected AdManagementPresenter createPresenter() {
        return new AdManagementPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ad_management;
    }

    @Override
    protected void initView() {
        initFragmentTabHost();
    }

    private void initFragmentTabHost() {
        String[] titles = getResources().getStringArray(R.array.my_ad_management_titles);
        Class[] fragments = new Class[]{AdFragment.class, AdFragment.class};
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        for (int i = 0; i < titles.length; i++) {
            View indicatorView = View.inflate(this, R.layout.tab_indicator_of_deal, null);
            TextView tvTitle = indicatorView.findViewById(R.id.tv_title);
            tvTitle.setText(titles[i]);
            tvTitle.setBackgroundResource(i == 0 ? R.drawable.selector_deal_tab_left : R.drawable.selector_deal_tab_right);
            Bundle bundle = new Bundle();
            bundle.putSerializable(AdFragment.EXTRA_TRADE_TYPE, i == 0 ? TradeType.SALE : TradeType.BUY);
            mTabHost.addTab(mTabHost.newTabSpec(titles[i]).setIndicator(indicatorView), fragments[i], bundle);
        }
        mTabHost.setOnTabChangedListener(this);
        mTabHost.setCurrentTab(0);
    }

    @Override
    protected void initData() {
        mPresenter.requestBankCardList(false);
        mPresenter.requestCoinTypeInfo(false);
        mPresenter.getBond();
    }

    @Override
    protected void initEvent() {
    }

    @OnClick({R.id.iv_back, R.id.tv_publish})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_publish:
                onPublishAd();
                break;
        }
    }

    private void onPublishAd() {
        if (!UserHelper.isSilver()) {
            showToast(getString(R.string.you_are_not_silver_permission));
            return;
        }

        if (TextUtils.isEmpty(UserHelper.getNickName())) {
            showSetNickNameDialog();
            return;
        }

        ArrayList<BankCardBean> bankCardBeans = mPresenter.getBankCardBeans();
        if (bankCardBeans == null) return;
        Bundle bundle = new Bundle();
        bundle.putSerializable(PublishAdvertisementActivity.EXTRA_TRADE_MODE, bankCardBeans);

        ArrayList<AdvertCoinTypeBean> advertCoinTypeBeans = mPresenter.getAdvertCoinTypeBeans();
        if (advertCoinTypeBeans == null) return;
        bundle.putSerializable(PublishAdvertisementActivity.EXTRA_COIN_TYPES, advertCoinTypeBeans);

        $startActivity(PublishAdvertisementActivity.class, bundle);
    }

    /**
     * 提示设置昵称对话框
     */
    private void showSetNickNameDialog() {
        if (mSetNickNameDialog == null) {
            mSetNickNameDialog = new ForbiddenBankCardDialog<String>(this)
                    .setMessage(getString(R.string.advertiser_hangs_a_bill_before_must_set_nickname_first))
                    .setOnConfirmClickListener(new ForbiddenBankCardDialog.OnConfirmClickListener<String>() {
                        @Override
                        public void onConfirm(String data) {
                            $startActivity(AccountInfoActivity.class);
                        }
                    });
        }
        mSetNickNameDialog.show();
    }

    /**
     * 还未设置收款方式
     */
    @Override
    public void onNotSetCollectionMode(String errMessage) {
        if (mMultipleDialog == null) {
            mMultipleDialog = new MultipleBlocksDialog(this);
        }
        mMultipleDialog.setTitle(getString(R.string.hint)).setContent(errMessage);
        mMultipleDialog.show();
        mMultipleDialog.setOnDialogItemClickListener(new MultipleBlocksDialog.OnDialogItemClickListener() {
            @Override
            public void onSure() {
                $startActivityForResult(BankCardActivity.class, REQUEST_CODE_SET_COLLECTION_MODE);
            }

            @Override
            public void onCancel() {
            }
        });
    }

    @Override
    public void setBond(MyBondBean bondBean) {
        if (bondBean != null)
            tvBond.setText(getString(R.string.ad_my_bond, StringUtil.handleZero(bondBean.bond), bondBean.currencyUnit));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_SET_COLLECTION_MODE) {
            mPresenter.requestBankCardList(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UIUtils.dismissDialog(mMultipleDialog);
        UIUtils.dismissDialog(mSetNickNameDialog);
    }

    @Override
    public void onTabChanged(String tabId) {
    }
}
